package ca.etsmtl.log430.lab3.donnee.txtreader;
import java.util.ArrayList;
import java.util.Collection;

import ca.etsmtl.log430.lab3.donnee.beans.TeacherBean;
import ca.etsmtl.log430.lab3.donnee.beans.TeacherBeanBuilder;
import ca.etsmtl.log430.lab3.donnee.ifaces.TeacherReader;

/**
 * @author  samuel
 */
public class FileTeacherReader extends LineOfTextFileReader implements TeacherReader {

	/**
	 * The list of teachers.
	 * @uml.property  name="listOfTeachers" multiplicity="(0 -1)"
	 */
	private Collection<TeacherBean> listOfTeachers = new ArrayList();

	public FileTeacherReader() {

		listOfTeachers = null;

	} // Constructor #1

	public FileTeacherReader(String InputFile) {

		listOfTeachers = readTeacherListFromFile(InputFile);

	} // Constructor #2

	/**
	 * Reads a line of text. The line of text is passed to the parseTeacherText
	 * method where it is parsed and a Teacher object is returned. The Teacher
	 * object is then added to the list. When a null is read from the Teacher
	 * file the method terminates and returns the list to the caller. A list
	 * that points to null is an empty list.
	 * 
	 * @param inputFile
	 * @return The list of teachers
	 */
	public Collection<TeacherBean> readTeacherListFromFile(String inputFile) {

		String teacherText; // Line of text from the file
		boolean done; // End of the file - stop processing

		// New teacher list object - this will contain all of the teachers in
		// the file

		ArrayList<TeacherBean> listObject = new ArrayList<TeacherBean>();

		if (openFile(inputFile)) {

			done = false;

			while (!done) {

				try {

					teacherText = readLineOfText();

					if (teacherText == null) {

						done = true;

					} else {

						listObject.add(parseTeacherText(teacherText));

					} // if

				} // try

				catch (Exception Error) {

					return (null);

				} // catch

			} // while

		} else {

			return (null);

		} // if

		return (listObject);

	} // readTeacherListFromFile

	/**
	 * @return
	 * @uml.property  name="listOfTeachers"
	 */
	@Override
	public Collection<TeacherBean> getListOfTeachers() {
		return listOfTeachers;
	}

	/**
	 * @param listOfTeachers
	 * @uml.property  name="listOfTeachers"
	 */
	public void setListOfTeachers(Collection<TeacherBean> listOfTeachers) {
		this.listOfTeachers = listOfTeachers;
	}

	/**
	 * Parse lines of text that are read from the text file containing teacher
	 * information in the above format.
	 * 
	 * @param lineOfText
	 * @return populated Teacher object
	 */
	private TeacherBean parseTeacherText(String lineOfText) {

		boolean done = false; // Indicates the end of parsing
		String token; // String token parsed from the line of text
		int tokenCount = 0; // Number of tokens parsed
		int frontIndex = 0; // Front index or character position
		int backIndex = 0; // Rear index or character position

		// Create a teacher object to record all of the info parsed from
		// the line of text
        TeacherBeanBuilder builder = new TeacherBeanBuilder();

		while (!done) {

			backIndex = lineOfText.indexOf(' ', frontIndex);

			if (backIndex == -1) {

				done = true;
				token = lineOfText.substring(frontIndex);

			} else {

				token = lineOfText.substring(frontIndex, backIndex);
			}

			switch (tokenCount) {

			case 0: // Teacher ID
				builder.setTeacherID(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;

			case 1: // Teacher's last name
				builder.setLastName(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;

			case 2: // Teacher's First name
				builder.setFirstName(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;

			case 3: // Teacher type (e.g. PRF, CHR)
				builder.setTeacherType(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;

			default:
				// This is where the courses are added to the teachers'
				// courses taught list within the teacher object
				// Note that there are no details other than the course
				// ID that is recorded in the teacher's courses taught
				// list.
				builder.addTaughtCoursesTaughtList(token);
				frontIndex = backIndex + 1;
				break;

			} // end switch

		} // end while

		return (builder.build());

	} // parseTeacherText

} // TeacherReader